package com.msh.job;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.msh.common.VMSystem;
import com.msh.entity.UserEntity;
import com.msh.service.UserService;
import com.msh.utils.JsonUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author :zhangchuanfu
 * @Date :2023/1/12
 * @Time :15:41
 * @description :定时初始化第二天，运维人员工单量
 */

@Component//需要交给spring管理
@Slf4j
public class UserJob {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @XxlJob("testHandler")
    public ReturnT<String> testHandler(String param) {
        log.info("继承xxl-job");
        XxlJobLogger.log("sdfsd");
        return ReturnT.SUCCESS;
    }
    @XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) {
        //查询用户列表,构建数据zet
        List<UserEntity> list = userService.list();
        list.forEach(user -> {
            if (user.getRoleId() != 0) {
                String key = VMSystem.REGION_TASK_KEY_PREF
                        + LocalDate.now().plusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"))
                        + "." + user.getRegionId() + "." + user.getRoleCode();

                redisTemplate.opsForZSet().add(key, user.getId(), 0);//存入userId和userName方便使用
                redisTemplate.expire(key, Duration.ofDays(2));
            }
        });
        return ReturnT.SUCCESS;
    }
}
